/*
 * Hisilicon SoC reset code
 *
 * Copyright (c) 2018 Fullhan Ltd.
 * Copyright (c) 2014 Linaro Ltd.
 *
 * Author: Haojian Zhuang <haojian.zhuang@linaro.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/delay.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>
#include <linux/reboot.h>
#include <linux/slab.h>
#include <asm/proc-fns.h>

struct protector {
	const char *name;
	void __iomem *base;
	u32 offset;
	u32 key;
};

static struct protector *fullhan_protector;
static void __iomem *base;
static u32 reboot_offset;
static u32 np_childs;

static void fh_set_scu_en(void)
{
	if (np_childs) {
		int i, offset;
		void __iomem *base;
		struct protector *protector = fullhan_protector;

		for (i = 0; i < np_childs; i++) {
			base = protector->base;
			offset = protector->offset;
			writel(protector->key, base + offset);
			protector++;
		}
	}
}

static void fh_set_scu_di(void)
{
	if (np_childs) {
		int i;
		struct protector *protector = fullhan_protector;

		for (i = 0; i < np_childs; i++) {
			writel(0x0, protector->base + protector->offset);
			protector++;
		}
	}
}


static int fh_restart_handler(struct notifier_block *this,
				unsigned long mode, void *cmd)
{

#if defined(CONFIG_ARCH_FH8862) || defined(CONFIG_ARCH_FH885xV300) \
	|| defined(CONFIG_ARCH_FH885xV310) || defined(CONFIG_ARCH_FH8866)
	fh_set_scu_en();
#endif
	writel_relaxed(0x7fffffff, base + reboot_offset);
#if defined(CONFIG_ARCH_FH8862) || defined(CONFIG_ARCH_FH885xV300) \
	|| defined(CONFIG_ARCH_FH885xV310) || defined(CONFIG_ARCH_FH8866)
	fh_set_scu_di();
#endif

	while (1)
		cpu_do_idle();

	return NOTIFY_DONE;
}

static struct notifier_block fh_restart_nb = {
	.notifier_call = fh_restart_handler,
	.priority = 128,
};

static int fh_reboot_probe(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node;
	struct device_node *child = NULL;
	int i, err;
	struct protector *protector = NULL;

	base = of_iomap(np, 0);
	if (!base) {
		WARN(1, "failed to map base address");
		return -ENODEV;
	}

	if (of_property_read_u32(np, "SWRST_MAIN_CTRL", &reboot_offset) < 0) {
		pr_err("failed to find SWRST_MAIN_CTRL property\n");
		iounmap(base);
		return -EINVAL;
	}

	err = register_restart_handler(&fh_restart_nb);
	if (err) {
		dev_err(&pdev->dev, "cannot register restart handler (err=%d)\n",
			err);
		iounmap(base);
	}

	np_childs = of_get_child_count(np);
	fullhan_protector = np_childs ?
		kzalloc(sizeof(struct protector) * np_childs, GFP_KERNEL) :
		NULL;
	protector = fullhan_protector;

	for (i = 0; i < np_childs; i++) {
		child = of_get_next_child(np, child);
		protector->base = of_iomap(child, 0);
		of_property_read_string(child, "name", &protector->name);
		of_property_read_u32(child, "keys", &protector->key);
		of_property_read_u32(child, "offset", &protector->offset);
		protector++;
	}

	return err;
}

static const struct of_device_id fh_reboot_of_match[] = {
	{ .compatible = "fh,fh-pmu" },
	{}
};

static struct platform_driver fh_reboot_driver = {
	.probe = fh_reboot_probe,
	.driver = {
		.name = "fh-reboot",
		.of_match_table = fh_reboot_of_match,
	},
};
module_platform_driver(fh_reboot_driver);
